<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="ordersList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
          width="600"
        ></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="100"
        ></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="primary" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="100"
        ></el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scope">{{
            scope.row.create_time | dataFormate('YYYY-MM-DD HH:mm:ss')
          }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showordresDialog(scope.row.order_id)"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showmsginfo"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 修改订单地址的对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="editAddressVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <el-form
          :model="editForm"
          :rules="editAddressRules"
          ref="editAddressRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address">
            <!-- 级联选择器 -->
            <el-cascader
              v-model="cityArr"
              :options="citydata"
              :props="cityProps"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="addressInfo">
            <el-input v-model.number="editForm.goods_price"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editAddressVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAddressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 物流信息对话框 -->
      <el-dialog
        title="查看物流信息"
        :visible.sync="progressVisible"
        width="50%"
      >
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progress"
            :key="index"
            icon="el-icon-more"
            :type="activity.type"
            color="pink"
            :timestamp="activity.ftime"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressVisible = false">取 消</el-button>
          <el-button type="primary" @click="progressVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// 导入省市区json数据
import citydata from './citydata'

export default {
  data() {
    return {
      // 获取订单列表的查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        user_id: '',
        is_send: '',
        consignee_addr: ''
      },
      // 订单列表的数组
      ordersList: [],
      // 订单的总数
      total: 0,
      // 修改地址的对话框的控制的布尔值
      editAddressVisible: false,
      // 获取订单列表的配置项
      addForm: {},
      // 修改订单列表的配置项
      editForm: {
        address: '',
        addressInfo: ''
      },
      // 修改订单列表地址的校验规则
      editAddressRules: {
        address: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        addressInfo: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 挂载数据
      citydata: citydata,
      // 级联选择框的配置信息
      cityProps: {
        expandTrigger: 'hover',
        label: 'label',
        value: 'value',
        children: 'children'
      },
      // 存储级联选择器的数据
      cityArr: [],
      // 控制物流信息的对话框的布尔值
      progressVisible: false,
      // 存储物流信息的数据
      progress: []
    }
  },
  methods: {
    // 修改每页显示多少条  pagesize发生改变触发的函数
    handleSizeChange(newVal) {
      this.queryInfo.pagesize = newVal
      // 重新发送请求
      this.getOrderList()
    },
    // 分页pagenum发生变化触发的方法
    handleCurrentChange(newVal) {
      this.queryInfo.pagenum = newVal
      // 重新发送请求
      this.getOrderList()
    },
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get(`orders`, {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品订单列表失败')
      }
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 监听对话框的关闭事件,重置对话框的表单项
    editDialogClosed() {
      this.$refs.editAddressRef.resetFields()
    },
    // 修改地址对话框
    showordresDialog() {
      this.editAddressVisible = true
    },
    // 查看物流信息的对话框
    async showmsginfo() {
      const { data: res } = await this.$http.get(`/kuaidi/1106975712662`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败,请稍后重试')
      }
      this.progress = res.data
      this.progressVisible = true
    },
    //
    handleChange() {
      console.log(this.cityArr)
    }
  },
  created() {
    // 调用订单列表的方法
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
